//
//  NSString+Attributed.m
//  WestWolves
//
//  Created by JL on 15/12/11.
//  Copyright © 2015年 ZJ. All rights reserved.
//

#import "NSString+Attributed.h"

@implementation NSString (Attributed)
+ (NSMutableAttributedString *)getMyPrice:(NSString *)price1 and:(UIFont *)font1 and:(UIColor *)color1 and:(NSString *)price2 and:(UIFont *)font2 and:(UIColor *)color2
{
    price1=[price1 stringByAppendingString:@"  "];
    NSMutableAttributedString *MApr1=[[NSMutableAttributedString alloc] initWithString:price1 attributes:[NSDictionary dictionaryWithObjectsAndKeys:font1,NSFontAttributeName,color1,NSForegroundColorAttributeName, nil]];
    NSMutableAttributedString *MApr2=[[NSMutableAttributedString alloc] initWithString:price2 attributes:[NSDictionary dictionaryWithObjectsAndKeys:font2,NSFontAttributeName,color2,NSForegroundColorAttributeName, nil]];
    [MApr2 addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:[price2 rangeOfString:price2]];
    [MApr1 appendAttributedString:MApr2];
    return MApr1;
}
- (NSMutableAttributedString *)AttributeWithFont:(int)font andColor:(UIColor *)color
{
    
    NSMutableAttributedString *attr=[[NSMutableAttributedString alloc] initWithString:self attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:font],NSFontAttributeName,color,NSForegroundColorAttributeName, nil]];
    return attr;
}
+ (NSMutableAttributedString *)appendAttributeWith:(NSArray *) fontNumArray andColor:(NSArray *)colorArray and:(NSString *)str
{
    NSMutableAttributedString *attribut=[[NSMutableAttributedString alloc] init];
    NSArray *strArray=[str componentsSeparatedByString:@";"];
    for (int i=0; i<strArray.count; i++) {
        NSMutableDictionary *attributDict=[[NSMutableDictionary alloc] init];
        NSString *subStr=[strArray objectAtIndex:i];
        if (fontNumArray.count>i) {
            NSString * fontNum =[fontNumArray objectAtIndex:i];
            if ([fontNum intValue]!=0) {
                [attributDict setObject:[UIFont systemFontOfSize:[fontNum floatValue]] forKey:NSFontAttributeName];
            }
        }
        if (colorArray.count>i) {
            id obj =[colorArray objectAtIndex:i];
            if ([obj isKindOfClass:[UIColor class]]) {
                [attributDict setObject:obj forKey:NSForegroundColorAttributeName];
            }
        }
        NSMutableAttributedString *subAttribute=[[NSMutableAttributedString alloc] initWithString:subStr attributes:attributDict];
        [attribut appendAttributedString:subAttribute];
    }
    return attribut;
}
- (NSMutableAttributedString *)getMyDiscountPriceWithColor:(UIColor *)color andFont:(int)fontNum
{
    NSMutableAttributedString *price=[[NSMutableAttributedString alloc] initWithString:self attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:fontNum],NSFontAttributeName,color,NSForegroundColorAttributeName, nil]];
    [price addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:[self rangeOfString:self]];
    return price;
}
- (NSMutableAttributedString *)hasPrefix:(NSString *)prefix andTextColor:(UIColor *)color andFont:(UIFont *)font
{
    NSMutableAttributedString *str=[[NSMutableAttributedString alloc] initWithString:self attributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil]];
    if ([self hasPrefix:prefix]) {
        [str addAttribute:NSForegroundColorAttributeName value:color range:[self rangeOfString:prefix]];
    }
    return str;
}

+ (NSMutableAttributedString *)appendAttributeWithStr:(NSArray *)strArray font:(NSArray *)fontNumArray andColor:(NSArray *)colorArray
{
    colorArray = colorArray.count==0?[NSArray arrayWithObject:[UIColor blackColor]]:colorArray;
    fontNumArray = fontNumArray.count ==0?@[@"15"]:fontNumArray;
    NSMutableAttributedString * attributString = nil;
    for (int i=0; i<strArray.count; i++) {
        NSString *str = strArray[i];
        UIColor * color = colorArray.count>i?colorArray[i]:[colorArray lastObject];
        NSString *fontNum = fontNumArray.count>i?fontNumArray[i]:[fontNumArray lastObject];
        if (attributString) {
            [attributString appendAttributedString:[str AttributeWithFont:[fontNum intValue] andColor:color]];
        }else{
            attributString = [str AttributeWithFont:[fontNum intValue] andColor:color];
        }
    }
    return attributString;
}

@end
